package CircuitLogic;
import static java.lang.Math.*;
/*
	用电阻箱实现的自组点位差计电路
 */
public class SelfAssembledPotentiometerByBox
{
	public StandardBattery standardBattery;
	public UnknownBattery unknownBattery;
	public DoubleThrowSwitch doubleThrowSwitch;
	public ResistanceBox resistanceBox1;
	public ResistanceBox resistanceBox2;
	public PointerGalvanometer pointerGalvanometer;
	public SingleThrowSwitch singleThrowSwitch;
	public VoltageStabilizedPowerSource voltageStabilizedPowerSource;

	SelfAssembledPotentiometerByBox()
	{
		standardBattery = new StandardBattery(22.5+5*random()-2.5);
		unknownBattery = new UnknownBattery();
		doubleThrowSwitch = new DoubleThrowSwitch();
		resistanceBox1 = new ResistanceBox();
		resistanceBox2 = new ResistanceBox();
		pointerGalvanometer = new PointerGalvanometer(10600);
		singleThrowSwitch = new SingleThrowSwitch();
		voltageStabilizedPowerSource = new VoltageStabilizedPowerSource();
	}

	/*
		刷新电路状态方法
		请在每一次更新状态后调用此方法刷新
		并更新每一个显示值
		显示值有：稳压电源的电压和电流，和检流器的指针
	 */
	public void updateAllState()
	{
		final double inf = 1e100;
		/*
			整个电路分为三个可能连通的回路
			共有5种情况
		 */
		if(
			voltageStabilizedPowerSource.getConnect() &&
			singleThrowSwitch.getConnect() &&
			singleThrowSwitch.getSwitchState() &&
			resistanceBox1.getConnect() &&
			resistanceBox2.getConnect() &&
			pointerGalvanometer.getConnect() &&
			doubleThrowSwitch.getConnect() &&
			(
				(doubleThrowSwitch.getSwitchState()==1 && unknownBattery.getConnect()) ||
				(doubleThrowSwitch.getSwitchState()==2 && standardBattery.getConnect())
			)
		)
		/*
			case1: 全部接通
		 */
		{
			double I2,I3;
			double R1,R2,R3;
			double E,Ex;
			double D;
			double RLoad;

			R1 = resistanceBox1.getResistance();
			R2 = resistanceBox2.getResistance();
			E = voltageStabilizedPowerSource.getVoltage();
			if(doubleThrowSwitch.getSwitchState()==1)
			{
				Ex = unknownBattery.getElectronicForce();
				R3 = unknownBattery.getResistance();
			}
			else
			{
				Ex = standardBattery.getElectronicForce();
				R3 = standardBattery.getResistance();
			}
			R3 += pointerGalvanometer.getResistance();
			D = R1*R2+R1*R3+R2*R3;
			I2 = ((R1+R3)*E-R1*Ex)/D;
			I3 = (R1*E-(R1+R2)*Ex)/D;
			RLoad = voltageStabilizedPowerSource.getOutputVoltage()/I2;

			pointerGalvanometer.setRealElectricity(I3);
			voltageStabilizedPowerSource.setResistanceLoad(RLoad);
		}

		else if
		(
			voltageStabilizedPowerSource.getConnect() &&
			singleThrowSwitch.getConnect() &&
			singleThrowSwitch.getSwitchState() &&
			resistanceBox1.getConnect() &&
			resistanceBox2.getConnect()
		)
		/*
			case2: 上部接通
		 */
		{
			double R1,R2;

			R1 = resistanceBox1.getResistance();
			R2 = resistanceBox2.getResistance();

			pointerGalvanometer.setRealElectricity(0);
			voltageStabilizedPowerSource.setResistanceLoad(R1+R2);
		}

		else if
		(
			voltageStabilizedPowerSource.getConnect() &&
			singleThrowSwitch.getConnect() &&
			singleThrowSwitch.getSwitchState() &&
			resistanceBox2.getConnect() &&
			pointerGalvanometer.getConnect() &&
			(
					(doubleThrowSwitch.getSwitchState()==1 && unknownBattery.getConnect()) ||
					(doubleThrowSwitch.getSwitchState()==2 && standardBattery.getConnect())
			)
		)
		/*
			case3: 上下接通
		 */
		{
			double I;
			double E,Ex;
			double R2,R3;

			if(doubleThrowSwitch.getSwitchState()==1)
			{
				Ex = unknownBattery.getElectronicForce();
				R3 = unknownBattery.getResistance();
			}
			else
			{
				Ex = standardBattery.getElectronicForce();
				R3 = standardBattery.getResistance();
			}
			E = voltageStabilizedPowerSource.getOutputVoltage();
			R3 += pointerGalvanometer.getResistance();
			R2 = resistanceBox2.getResistance();
			I = (E-Ex)/(R2+R3);

			pointerGalvanometer.setRealElectricity(I);
			voltageStabilizedPowerSource.setResistanceLoad(E/I);
		}

		else if
		(
			resistanceBox1.getConnect() &&
			pointerGalvanometer.getConnect() &&
			(
					(doubleThrowSwitch.getSwitchState()==1 && unknownBattery.getConnect()) ||
					(doubleThrowSwitch.getSwitchState()==2 && standardBattery.getConnect())
			)
		)
		/*
			cse4: 下部接通
		 */
		{
			double I;
			double Ex;
			double R1,R3;

			if(doubleThrowSwitch.getSwitchState()==1)
			{
				Ex = unknownBattery.getElectronicForce();
				R3 = unknownBattery.getResistance();
			}
			else
			{
				Ex = standardBattery.getElectronicForce();
				R3 = standardBattery.getResistance();
			}
			R3 += pointerGalvanometer.getResistance();
			R1 = resistanceBox1.getResistance();
			I = -Ex/(R1+R3);

			pointerGalvanometer.setRealElectricity(I);
			voltageStabilizedPowerSource.setResistanceLoad(inf);
		}

		else
		/*
			case5: 全不连通
		 */
		{
			pointerGalvanometer.setRealElectricity(0);
			voltageStabilizedPowerSource.setResistanceLoad(inf);
		}
	}

}